/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.football.lottery.services;

import com.football.lottery.util.OrderCombination;
import com.football.lottery.util.ResultCombination;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author JingWen.Wen
 */
public class CombinServices
{
   public List genBetList(Map betMap, Map preBetMap, Set preNos)
  {
    // 先主
    List betList = this.betMaptoBetList(betMap);
    List resultList = ResultCombination.getResultCombination(betList);
    
    // 后补
    List preBetOrderNos = new ArrayList(preBetMap.keySet());
    List orderNoResult = new ArrayList();
    Iterator preNoIt = preNos.iterator();
    while (preNoIt.hasNext())
    {
      int length = (Integer) preNoIt.next();
      orderNoResult.addAll(OrderCombination.getCombinations(preBetOrderNos, length));
    }
    
    // Replace And Combin
    Iterator it = orderNoResult.iterator();
    while (it.hasNext())
    {
      Map newBetMap = new HashMap(betMap);
      List orderList = (List) it.next();
      Iterator orderIt = orderList.iterator();
      while (orderIt.hasNext())
      {
        int orderNo = (Integer) orderIt.next();
        // Replace bet to preventive bet.
        newBetMap.put(orderNo, (String) preBetMap.get(orderNo));
      }
      List newBetList = this.betMaptoBetList(newBetMap);
      resultList.addAll(ResultCombination.getResultCombination(newBetList));
    }
    return resultList;
  }

  private List betMaptoBetList(Map betMap)
  {
    List orderNoList = new ArrayList(betMap.keySet());
    List betList = new ArrayList();
    Collections.sort(orderNoList);
    Iterator orderNoIt = orderNoList.iterator();
    while (orderNoIt.hasNext())
    {
      int orderNo = (Integer) orderNoIt.next();
      String bet = (String) betMap.get(orderNo);
      betList.add(bet);
    }
    return betList;
  }
  
}
